import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';

import '../../daily_mission_report_module/my_mission_module/my_mission_view.dart';
import '../../my_project_report_module/my_project_module/my_project_view.dart';
import 'package:intl/intl.dart' as Datee;

class CreateTimeSheetController extends GetxController{


  TextEditingController editingController = TextEditingController();


  var startTime = '请选择开始时间';
  var projectName = '请选择项目';
  var projectId;

  var taskName = '请选择任务';
  var taskId;



  /// 选择时间
  tapClickStartTime(){
    DatePicker.showDatePicker(Get.context!,
        showTitleActions: true,
        minTime: DateTime(1990, 1, 1),
        maxTime: DateTime(2025, 12, 12), onChanged: (date) {

        }, onConfirm: (date) {
          print('${date.year}-${date.month}-${date.day}');

          var timeFormat = Datee.DateFormat("yyyy-MM-dd");
          var timeStr = timeFormat.format(date);

          startTime = timeStr;

          // controller.
          // requestDataWithUpdataInfo(alias_name: controller.editingController.text,
          //     sex: '女',birthday: '${date.year}-${date.month}-${date.day}');
          print('confirm $date');
          update();
        }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  /// 选择项目
  tapClickProject()async{
    var data = await Get.to(MyProjectView(type: 'relevancy',));
    if(data != null){
      projectName = data['name'];
      projectId = data['id'];
    }
    update();
  }
  /// 选择任务
  tapClickTask()async{
    var data = await Get.to(MyMissionView(type: 'relevancy',));
    if(data != null){
      taskName = data['name'];
      taskId = data['id'];
    }
    update();
  }

  requestDataWithCreate()async{

    var params  = {
      'name':editingController.text,
      'date':startTime,
      'project_id':projectId,
      'task_id':taskId,
      // 'work_hours':'00:10',
      'account_analytic_line_id':'0'  //工时单ID，0-新增
    };
    
    var json = await DioManager().kkRequest(Address.createTimeSheet,bodyParams: params);

    if(json['errmsg']=='ok'){
      Get.back(result: 'refresh');
    }
    BotToast.showText(text: json['message']);
  }

}